﻿Library.UserInterface.Widgets.Currency = function (options) {
    var defaults = {
        container: null,
        interval: 320000,
        loader: "Yükleniyor...",
        titleSelector: "",        
        onSettingsSave: ""
    };
    var options = $.extend(defaults, options);

    this.container = options.container;
    this.interval = options.interval;
    this.loader = options.loader;
    this.titleSelector = options.titleSelector;
    this.onSettingsSave = options.onSettingsSave;

    this.initialize(false);
}

Library.UserInterface.Widgets.Currency.prototype = {
    container: null,
    interval: 320000,
    loader: "",
    titleSelector: "",
    onSettingsSave: "",
    fullscreen: false,
    minimize: true,
    settings: false,
    id: null,
    type: "widget.currency",

    initialize: function (set) {
        if (this.id != null)
            Library.UserInterface.Widgets.Dispose(this.id);

        this.id = Library.Util.ReplaceAll(Library.Crypto.Guid(), "-", "");
        this.container.find(".portlet-widget").html(this.loader);

        Library.UserInterface.Widgets.Add(this);

        if (this.titleSelector == "")
            this.titleSelector = this.container.find(".portlet-title").selector;

        if (this.titleSelector != "")
            $(this.titleSelector).html(this.loader);

        var widget = this;
        try {
            $(document).everyTime(this.interval, this.id, function (i) {
                $.get("/Util/XmlDoor.aspx", { Url: "http://xml.altinkaynak.com.tr/doviz.xml" },
                    function (data) {
                        if (widget.titleSelector != "")
                            $(widget.titleSelector).html("Döviz Kurları");

                        var html = "";
                        html += "<table cellpadding='0' cellspacing='0' border='0' width='100%' style='text-align:left; font-size:12px'>";
                        html += "<tr><td colspan='3' class='DovizHeader'>AltınKaynak - Son Güncelleme: <b>{0}</b></td></tr>";
                        html += "<tr><td class='DovizTitle' style='font-weight:bold'>Para Birimi</td><td class='DovizAlis' style='font-weight:bold'>Alış</td><td class='DovizSatis' style='font-weight:bold'>Satış</td></tr>";
                        html += "<tr><td class='DovizTitle'>Amerikan Doları</td><td class='DovizAlis'>{1}</td><td class='DovizSatis'>{2}</td></tr>";
                        html += "<tr><td class='DovizTitle'>Avrupa Para Birimi</td><td class='DovizAlis'>{3}</td><td class='DovizSatis'>{4}</td></tr>";
                        html += "<tr><td class='DovizTitle'>İngiliz Sterlini</td><td class='DovizAlis'>{5}</td><td class='DovizSatis'>{6}</td></tr>";
                        html += "<tr><td class='DovizTitle'>İsviçre Frangı</td><td class='DovizAlis'>{7}</td><td class='DovizSatis'>{8}</td></tr>";
                        html += "<tr><td class='DovizTitle'>Danimarka Kronu</td><td class='DovizAlis'>{9}</td><td class='DovizSatis'>{10}</td></tr>";
                        html += "<tr><td class='DovizTitle'>İsveç Kronu</td><td class='DovizAlis'>{11}</td><td class='DovizSatis'>{12}</td></tr>";
                        html += "<tr><td class='DovizTitle'>Norveç Kronu</td><td class='DovizAlis'>{13}</td><td class='DovizSatis'>{14}</td></tr>";
                        html += "<tr><td class='DovizTitle'>Japon Yeni</td><td class='DovizAlis'>{15}</td><td class='DovizSatis'>{16}</td></tr>";
                        html += "<tr><td class='DovizTitle'>Suudi Arabistan Riyali</td><td class='DovizAlis'>{17}</td><td class='DovizSatis'>{18}</td></tr>";
                        html += "<tr><td class='DovizTitle'>Avusturalya Doları</td><td class='DovizAlis'>{19}</td><td class='DovizSatis'>{20}</td></tr>";
                        html += "<tr><td class='DovizTitle'>Kanada Doları</td><td class='DovizAlis'>{21}</td><td class='DovizSatis'>{22}</td></tr>";
                        html += "</table>";

                        html = html.replace("{0}", $($(data).find("DOVIZ")[0]).find("ALIS").text());
                        html = html.replace("{1}", $($(data).find("DOVIZ")[1]).find("ALIS").text());
                        html = html.replace("{2}", $($(data).find("DOVIZ")[1]).find("SATIS").text());
                        html = html.replace("{3}", $($(data).find("DOVIZ")[2]).find("ALIS").text());
                        html = html.replace("{4}", $($(data).find("DOVIZ")[2]).find("SATIS").text());
                        html = html.replace("{5}", $($(data).find("DOVIZ")[4]).find("ALIS").text());
                        html = html.replace("{6}", $($(data).find("DOVIZ")[4]).find("SATIS").text());
                        html = html.replace("{7}", $($(data).find("DOVIZ")[3]).find("ALIS").text());
                        html = html.replace("{8}", $($(data).find("DOVIZ")[4]).find("SATIS").text());
                        html = html.replace("{9}", $($(data).find("DOVIZ")[5]).find("ALIS").text());
                        html = html.replace("{10}", $($(data).find("DOVIZ")[5]).find("SATIS").text());
                        html = html.replace("{11}", $($(data).find("DOVIZ")[6]).find("ALIS").text());
                        html = html.replace("{12}", $($(data).find("DOVIZ")[6]).find("SATIS").text());
                        html = html.replace("{13}", $($(data).find("DOVIZ")[7]).find("ALIS").text());
                        html = html.replace("{14}", $($(data).find("DOVIZ")[7]).find("SATIS").text());
                        html = html.replace("{15}", $($(data).find("DOVIZ")[8]).find("ALIS").text());
                        html = html.replace("{16}", $($(data).find("DOVIZ")[8]).find("SATIS").text());
                        html = html.replace("{17}", $($(data).find("DOVIZ")[9]).find("ALIS").text());
                        html = html.replace("{18}", $($(data).find("DOVIZ")[9]).find("SATIS").text());
                        html = html.replace("{19}", $($(data).find("DOVIZ")[10]).find("ALIS").text());
                        html = html.replace("{20}", $($(data).find("DOVIZ")[10]).find("SATIS").text());
                        html = html.replace("{21}", $($(data).find("DOVIZ")[11]).find("ALIS").text());
                        html = html.replace("{22}", $($(data).find("DOVIZ")[11]).find("SATIS").text());

                        widget.container.find(".portlet-widget").html(html);
                    }
                );
            });
            Library.UserInterface.Widgets.SetHeaderIcons(this);
        } catch (err) {
            Library.UserInterface.Widgets.Error(this.id, err);
        }
    }
}